﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using CloudShot.Settings;
using CloudShot.Utils;

namespace CloudShot.Services
{
	internal sealed class ConfigurationService
	{
		private static readonly object _sync = new object();

		private CloudShotSettings DefaultSettings
		{
			get
			{
				return new CloudShotSettings
				{
					IsCaptureCursorOnGif = true,
					IsCopyImage = false,
					IsCopyUrl = true,
					IsCopyRecentShotPath = false,
					IsEnableSounds = true,
					IsOpenInBrowser = true,
					IsShowActiveStorageName = true,
					IsShowNotifications = true,
					IsStartWithComputer = true,
					ShotHotkey = "PrintScreen",
					RecordHotkey = "Ctrl+PrintScreen",
					FollowMouseHotkey = "F9",
					RecordingTimeInSeconds = 10
				};
			}
		}

		public CloudShotSettings ReadApplicationSettings()
		{
			CloudShotSettings settings;

			var configInfo = new FileInfo(AppHelper.ConfigFile);
			var defaultConfigInfo = new FileInfo(AppHelper.DefaultConfig);

			if (configInfo.Exists && configInfo.Length > 0)
			{
				settings = ReadSettings(configInfo.FullName);
			}
			else if (defaultConfigInfo.Exists && defaultConfigInfo.Length > 0)
			{
				settings = ReadSettings(defaultConfigInfo.FullName);
			}
			else
			{
				settings = DefaultSettings;
			}

			UpdateStartupShortcut(settings.IsStartWithComputer);

			return settings;
		}

		public void WriteApplicationSettings(CloudShotSettings settings)
		{
			if (settings == null)
				throw new ArgumentNullException("settings");

			WriteSettings(AppHelper.ConfigFile, settings);
			UpdateStartupShortcut(settings.IsStartWithComputer);
		}

		private CloudShotSettings ReadSettings(string fileName)
		{
			lock (_sync)
			{
				using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
				{
					var serializer = new XmlSerializer(typeof(CloudShotSettings));
					var settings = (CloudShotSettings)serializer.Deserialize(stream);
					return settings;
				}
			}
		}

		private void WriteSettings(string fileName, CloudShotSettings settings)
		{
			lock (_sync)
			{
				using (var stream = File.Create(fileName))
				{
					var serializer = new XmlSerializer(typeof(CloudShotSettings));
					serializer.Serialize(stream, settings);
					stream.Flush();
				}
			}


			//var xmlData = new StringBuilder();

			//using (XmlWriter xw = XmlWriter.Create(xmlData, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }))
			//{
			//	var dcs = new DataContractSerializer(typeof(CloudShotSettings));
			//	dcs.WriteObject(xw, settings);
			//}

			//lock (_sync)
			//{
			//	using (var stream = File.CreateText(fileName))
			//	{
			//		stream.Write(xmlData.ToString());
			//		stream.Flush();
			//	}
			//}
		}

		private void UpdateStartupShortcut(bool startWithComputer)
		{
			lock (_sync)
			{
				RegistryUtils.SetStartup(Application.ProductName, startWithComputer);
			}
		}
	}
}